package user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import user.db.DBConnection;
import user.model.Book;
import user.model.SystemAdmin;
import user.model.User;

public class SystemAdminDao {
	//系统管理员登录验证
			public SystemAdmin systemAdminLogin(Connection con, SystemAdmin adminMessage) throws Exception {
			
				SystemAdmin resultAdmin = null;
				String sql = "select * from systemAdmin where systemAdminName = ? and password = ?";
				PreparedStatement pstmt = con.prepareStatement(sql);
				pstmt.setString(1,adminMessage.getAdminName());
				pstmt.setString(2,adminMessage.getPassword());
				ResultSet rs = pstmt.executeQuery();
				
		 		if(rs.next()) {
					resultAdmin = new SystemAdmin();
				    resultAdmin.setAdminId(rs.getString("systemAdminId"));
					resultAdmin.setAdminName(rs.getString("systemAdminName"));
					resultAdmin.setPassword(rs.getString("password"));
				}	
		 		
				return resultAdmin;
			}
			/**
			 * 实现添加用户功能
			 * @param book
			 * @return
			 * @throws Exception
			 */
			public int addUser(User user) throws Exception {
				Connection conn = DBConnection.getConnection();
				//先在数据库中查询主键id，若查到了，返回0，代表注册失败
				String sqlid = "select * from user where id = ?";
				PreparedStatement pstmtid = conn.prepareStatement(sqlid);
				pstmtid.setString(1, user.getId());
				ResultSet rsid = pstmtid.executeQuery();
				if(rsid.next()){
					return 0;
				}
				//若数据库中没有id，则可以插入
				String sql = "insert into user values(?,?,?)";
				PreparedStatement ps = null;
				try {
					ps = conn.prepareStatement(sql);
					ps.setString(1, user.getId());
					ps.setString(2, user.getUserName());
					ps.setString(3, user.getPassword());
				} catch (Exception e) {
					e.printStackTrace();
				}
				return ps.executeUpdate();
			}
			
			/**
			 * 删除用户功能
			 * @param userId
			 * @return
			 * @throws Exception
			 */
			public int deleteUser(String userId) throws Exception {
				Connection con = DBConnection.getConnection();
				String sql = "delete from user where id = ?";
				PreparedStatement ps = null;
				ResultSet rs = null;
				try {
					ps = con.prepareStatement(sql);
					ps.setString(1,userId);
				}catch (Exception e) {
					e.printStackTrace();
				}
				return ps.executeUpdate();
			}
			
			/**
			 * 修改用户功能
			 * @param user
			 * @return
			 * @throws Exception
			 */
			public int updateUser(User user) throws Exception {
				Connection conn = DBConnection.getConnection();
				String sql = "update user set userName = ?,password = ? where id = ?";
				PreparedStatement ps = null;
				ResultSet rs = null;
				try {
					ps = conn.prepareStatement(sql);
					ps.setString(1, user.getUserName());
					ps.setString(2, user.getPassword());
					ps.setString(3, user.getId());
					}
					catch (Exception e) {
					e.printStackTrace();
					}
				return ps.executeUpdate();
			}
			
			/**
			 * 查询用户功能
			 * @param userId
			 * @param userName
			 * @return
			 */
			public List<User> User(String userId,String userName){
				String sql = "select * from user where id like ? and userName like ? ";
				PreparedStatement ps = null;
				ResultSet rs = null;
				Connection connection = DBConnection.getConnection();
				List<User> list = new ArrayList<User>();
				try {
					ps = connection.prepareStatement(sql);
					ps.setString(1, "%"+userId+"%");
					ps.setString(2, "%"+userName+"%");
					rs = ps.executeQuery();
					while(rs.next()) {
						String id = rs.getString("id");
						String name = rs.getString("userName");
						String password = rs.getString("password");
						User user = new User(id, name, password);
						list.add(user);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return list;
			}
			
			public int updatePwd(String newpwd1, String loginName) {
				Connection con = DBConnection.getConnection();
				PreparedStatement ps = null;
				ResultSet rs = null;
				String sql="update systemadmin set password=? where systemadminname=?";
				int i = 0;
				try {
					ps = con.prepareStatement(sql);
					ps.setString(1, newpwd1);
					ps.setString(2, loginName);
					i = ps.executeUpdate();
				} catch (Exception e) {
					e.printStackTrace();
				}finally {
					DBConnection.close(rs, ps);
				}
				return i;
			}
}
